// import { getToken, setToken, removeToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'
import { login, getUserInfo, logout } from '@/api/user'
import { allAsyncRoutes, anyRoute, staticRoutes } from '@/router/routes'
import { cloneDeep } from 'lodash'

const filterUserAsyncRoutes = (userRoutes, allAsyncRoutes) => {
  return allAsyncRoutes.filter(item => {
    if (userRoutes.indexOf(item.name) !== -1) {
      if (item.children?.length) {
        item.children = filterUserAsyncRoutes(userRoutes, item.children)
      }
      return true
    }
  })
}

const initUserInfo = () => ({
  username: '',
  avatar: '',
  routes: [],
  buttons: [],
  roles: []
})

const state = {
  userInfo: {
    username: '',
    avatar: '',
    routes: [],
    buttons: [],
    roles: []
  },
  token: localStorage.getItem('token_key') || ''
}

const mutations = {
  SET_USERINFO(state, userInfo) {
    state.userInfo = userInfo
  },
  SET_TOKEN(state, token) {
    state.token = token
  },
  RESET_USERINFO(state) {
    // Object.assign(state.userInfo, initUserInfo())
    state.userInfo = initUserInfo()
    state.menuRoutes = staticRoutes.concat(anyRoute)
  },
  RESET_TOKEN(state) {
    state.token = ''
  }
}

const actions = {
  // user login
  async login({ commit }, userInfo) {
    try {
      const result = await login(userInfo)
      commit('SET_TOKEN', result.token)
      localStorage.setItem('token_key', result.token)
      return 'ok'
    } catch (error) {
      return Promise.reject(error)
    }
  },

  // get user info
  async getInfo({ commit, state }) {
    try {
      const result = await getUserInfo()
      // console.log(result)
      commit('SET_USERINFO', result)
      const userAsyncRoutes = filterUserAsyncRoutes(cloneDeep(state.userInfo.routes), allAsyncRoutes)
      // console.log(userAsyncRoutes)
      // asyncAddRoutes(userAsyncRoutes.concat(anyRoute))
      // router.addRoutes(userAsyncRoutes.concat(anyRoute))
      // router.addRoutes(anyRoute)
      router.onReady(() => {
        router.addRoutes([...userAsyncRoutes, anyRoute])
        router.options.routes = [...router.options.routes, ...userAsyncRoutes, anyRoute]
      })
      // console.log([...userAsyncRoutes, anyRoute])
      // router.addRoutes(userAsyncRoutes)
      // console.log(userAsyncRoutes.concat(anyRoute))
      // console.log(router.options)
      // commit('SET_MENUROUTES', staticRoutes.concat(userAsyncRoutes, anyRoute))
      return 'ok'
    } catch (error) {
      return Promise.reject(error)
    }
  },

  // user logout
  async logout({ commit }) {
    try {
      await logout()
      commit('RESET_USERINFO')
      commit('RESET_TOKEN')
      localStorage.removeItem('token_key')
      resetRouter()
      router.options.routes = [...staticRoutes]
      console.log(router)
      return 'ok'
    } catch (error) {
      return Promise.reject(error)
    }
  },

  // remove token
  reset({ commit }) {
    commit('RESET_TOKEN')
    commit('RESET_USERINFO')
    return 'ok'
  }
}

const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}

